package com.study.zzedu.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.ExercisesItem;
import com.study.zzedu.entity.LessonHeaderModel;
import com.study.zzedu.entity.LessonItem;
import com.study.zzedu.fragment.home.mycourse.HomeFragment;
import com.study.zzedu.ui.study.StudyActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.StringUtils;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.ProgressBarView;

import java.util.List;

/**
 * Created by kiros on 2019/7/16.
 * 首页item的adapter
 */

public class LessonAdapter extends RecyclerView.Adapter {

    private HomeFragment context;
    private List data;
    private Activity activity;
    private final static int HEAD_COUNT = 1;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;

    @Override
    public int getItemViewType(int position) {
//        if (HEAD_COUNT != 0 && position == 0) { // 头部
//            return TYPE_HEAD;
//        } else
        {
            return TYPE_CONTENT;
        }
    }

    public LessonAdapter(HomeFragment context, Activity activity, List data) {
        this.context = context;
        this.activity = activity;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        {
            View itemView = LayoutInflater.from(activity).inflate(R.layout.layout_lesson_item, parent, false);
            return new ContentHolder(itemView);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) { // 内容
            final LessonItem item = (LessonItem) data.get(position);

            ContentHolder myHolder = (ContentHolder) holder;
            myHolder.lessonName.setText(item.getLessonName());
            myHolder.tv_course_name.setText(item.getLessonName());

            myHolder.credit.setText((int) (item.getScore()) + "学分");
            switch (item.getStatus()) {
                case "学习中":
                    myHolder.status.setBackgroundResource(R.mipmap.zu2);
                    break;
                case "已学完":
                    myHolder.status.setBackgroundResource(R.mipmap.zu_green);
                    break;
                case "未学习":
                    myHolder.status.setBackgroundResource(R.mipmap.zu3);
                    break;
                case "未开放":
                    myHolder.status.setBackgroundResource(R.mipmap.zu4);
                    break;
            }
            myHolder.status.setText(item.getStatus());
            myHolder.tv_lastLearnginTime.setText(item.getLastLearnginTime());
            //myHolder.imgUrl.setBackground(context.getResources().getDrawable(R.mipmap.makesi, null));
            if (TextUtils.isEmpty(item.getFinalScore())) {
                myHolder.tv_finalScore.setVisibility(View.INVISIBLE);
                myHolder.finalScore.setVisibility(View.INVISIBLE);
            } else {
                if (StringUtils.isNumeric(item.getFinalScore())) {
                    myHolder.finalScore.setText(item.getFinalScore());
                    myHolder.finalScore.setVisibility(View.VISIBLE);
                    myHolder.tv_finalScore.setVisibility(View.INVISIBLE);
                } else {
                    myHolder.tv_finalScore.setText(item.getFinalScore());
                    myHolder.tv_finalScore.setVisibility(View.VISIBLE);
                    myHolder.finalScore.setVisibility(View.INVISIBLE);
                }
            }
            if(item.getLessonName().contains("统考") || item.getLessonName().contains("毕业")){
                myHolder.course_schedule.setText("0.0","0.0");
                myHolder.course_schedule.setProgress(0);
                myHolder.progress_test.setText("0.0","0.0");
                myHolder.progress_test.setProgress(0);
            }else {
                myHolder.course_schedule.setText(String.valueOf(item.getProgress()),String.valueOf(item.getCourseTotal()));
                myHolder.course_schedule.setProgress((int) ((item.getProgress()) / (item.getCourseTotal()) * 100));
                myHolder.progress_test.setText(String.valueOf(item.getOnlineTest()),String.valueOf(item.getTestTotal()));
                myHolder.progress_test.setProgress((int) ((item.getOnlineTest()) / (item.getTestTotal()) * 100));
            }

            myHolder.tv_curTerm.setText(item.getCurTerm());
            myHolder.ll_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if("未开放".equals(item.getStatus())){
                        ToastUtil.showShortToast("该课程尚未开放在线学习");
                        return;
                    }
                    Intent intent = new Intent(activity, StudyActivity.class);
                    intent.putExtra("courseCode", item.getCourseCode());
                    //intent.putExtra("chapterCode", item.get());
                    intent.putExtra("lessonName", item.getLessonName());
                    intent.putExtra("score", String.valueOf(item.getScore()));
                    intent.putExtra("examType", item.getExamType());
                    intent.putExtra("totalHour", item.getTotalHour());
                    intent.putExtra("from", "course");
                    context.startActivityForResult(intent, AppConstant.RequestCode_HomeToStudyActivity);
                }
            });
        } else { // 尾部

        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    //内容
    public class ContentHolder extends RecyclerView.ViewHolder {

        private LinearLayout ll_item;
        private TextView lessonName, tv_curTerm, tv_lastLearnginTime, tv_allowExam, tv_allowExam2;
        private ImageView imgUrl;
        private TextView credit,tv_course_name;
        private TextView status, finalScore, tv_finalScore;
        private ProgressBarView course_schedule, progress_test;

        public ContentHolder(View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            lessonName = itemView.findViewById(R.id.lesson_name);
            tv_course_name = itemView.findViewById(R.id.tv_course_name);

            imgUrl = itemView.findViewById(R.id.lesson_img);
            tv_finalScore = itemView.findViewById(R.id.tv_finalScore);
            finalScore = itemView.findViewById(R.id.lesson_img_score);
            credit = itemView.findViewById(R.id.tv_my_credit);
            status = itemView.findViewById(R.id.tv_status);

            course_schedule = itemView.findViewById(R.id.course_schedule);
            course_schedule.setText("-","-");
            progress_test = itemView.findViewById(R.id.progress_test);
            progress_test.setText("-","-");

            tv_curTerm = itemView.findViewById(R.id.tv_curTerm);
            tv_lastLearnginTime = itemView.findViewById(R.id.tv_lastLearnginTime);
            tv_allowExam = itemView.findViewById(R.id.tv_allowExam);
            tv_allowExam2 = itemView.findViewById(R.id.tv_allowExam2);
        }
    }
}
